//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    imgurl: app.urlimg,
    isEdit: false,
    allImg: "/pages/icon/checkMsg.png",
    isAllImg: false,
    selectGoodsImgs: [],
    data: [],//收藏+猜你喜欢列表

  },
  onLoad: function (e) {
    var that = this;


    app.app_load(load_fn);

    function load_fn() {
      wx.showLoading({
        title: '加载中...',
      });

      wx.request({
        url: app.url + 'api_team/get_favoriteV1',
        data: {
        },
        header: {
          'content-type': 'application/json',
          'token': wx.getStorageSync('token_key')
        }, success: function (json) {
          wx.hideLoading();
          var selectGoodsImgs = [];
          for (var i = 0; i < json.data.result.favorite_goods.length; i++) {
            selectGoodsImgs.push(
              "/pages/icon/checkMsg.png"
            )
          }
          that.setData({
            data: json.data.result,
            selectGoodsImgs: selectGoodsImgs,
          });
          console.log(that.data);
        }
      })
    }

  },

  // 响应编辑 
  editTap: function () {
    var that = this;
    that.setData({ isEdit: !that.data.isEdit, });
  },


  // 响应选中商品
  selectGoodsTap: function (e) {

    var that = this;
    var cid = e.currentTarget.id;
    var selectGoodsImgs = that.data.selectGoodsImgs;

    for (var i = 0; i < selectGoodsImgs.length; i++) {

      if (i == cid) {

        var img = "/pages/icon/checkMsg1.png";
        selectGoodsImgs[i] = selectGoodsImgs[i] == img ? "/pages/icon/checkMsg.png" : "/pages/icon/checkMsg1.png";

      }
    }

    that.setData({ selectGoodsImgs: selectGoodsImgs, });

  },
  goodslink: function (e) {
    var url ="";
    if(e.currentTarget.dataset.group_type){
      url = "/pages/group/groupdetail/index?id=" + e.currentTarget.id;
    }else{
      url = '/pages/index/detail/index?id=' + e.currentTarget.id;
    }
    wx.redirectTo({
      url: url,
    })
  },
  // 响应全选
  allTap: function () {

    var that = this;

    var isAllImg = !that.data.isAllImg;
    var allImg = isAllImg == true ? "/pages/icon/checkMsg1.png" : "/pages/icon/checkMsg.png";

    var selectGoodsImgs = that.data.selectGoodsImgs;

    for (var i = 0; i < selectGoodsImgs.length; i++) {

      selectGoodsImgs[i] = isAllImg == true ? "/pages/icon/checkMsg1.png" : "/pages/icon/checkMsg.png";
    }

    that.setData({
      isAllImg: isAllImg,
      allImg: allImg,
      selectGoodsImgs: selectGoodsImgs,
    });

  },



  // 删除商品
  deleteTAP: function () {

    var that = this;
    var goods = that.data.data.favorite_goods;
    var selectGoodsImgs = that.data.selectGoodsImgs;
    console.log(that.data);
    console.log(goods);

    //  判断是否全选
    if (that.data.isAllImg == true) {

      // 修复全选删除Bug.
      while (goods.length > 0) {
        // 调用删除商品方法
        deleteGoods(that, goods, selectGoodsImgs);
      }

    } else {

      // 如果不是全选
      // 调用删除商品方法
      // 修复一次删除多个商品,有一个不能删除Bug
      for (var i = 0; i < 2; i++) {
        deleteGoods(that, goods, selectGoodsImgs);
      }
    }

  },


  //猜你喜欢，查看商品详情
  click_goods: function (e) {
    console.log(e.currentTarget.id);
    wx.redirectTo({
      url: '/pages/index/detail/index?id=' + e.currentTarget.id,
    })
  },
})


// 删除商品
function deleteGoods(that, goods, selectGoodsImgs) {
  var img = "/pages/icon/checkMsg1.png";
  var id = ''
  for (var i = 0; i < that.data.data.favorite_goods.length; i++) {
    if (selectGoodsImgs[i] == img) {
      id += that.data.data.favorite_goods[i].goods.id + ',';
      goods.splice(i, 1);
      selectGoodsImgs.splice(i, 1);
    }
  }
  console.log(id);
  wx.request({
    url: app.url + 'api_goods/del_favorite?goods_id',
    data: {
      goods_id: id
    },
    header: {
      'content-type': 'application/json',
      'token': wx.getStorageSync('token_key')
    }, success: function (json) {
      console.log(json)
      that.setData({ 'data.favorite_goods': goods, selectGoodsImgs: selectGoodsImgs })
    }
  })
}

